package com.vvv.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vvv.common.ErrorCode;
import com.vvv.constants.UserConstant;
import com.vvv.exception.BusinessException;
import com.vvv.mapper.UserMapper;
import com.vvv.model.entity.User;
import com.vvv.model.vo.LoginUserVo;
import com.vvv.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
* @author v
* @description 针对表【user(用户)】的数据库操作Service实现
* @createDate 2023-12-08 08:59:26
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService {


    @Override
    public LoginUserVo login(String userAccount, String userPassword, HttpServletRequest request) {
        if (StringUtils.isAnyEmpty(userAccount, userPassword)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "account and password can not be empty");
        }
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUserAccount, userAccount);
        queryWrapper.eq(User::getUserPassword, userPassword);
        User user = getOne(queryWrapper);
        if (user == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "user is not exist");
        }
        request.getSession().setAttribute(UserConstant.USER_CURRENT_STATE, user);
        return BeanUtil.copyProperties(user, LoginUserVo.class);
    }

    @Override
    public long register(String userAccount, String userPassword) {
        if (StringUtils.isAnyEmpty(userAccount, userPassword)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "account and password can not be empty");
        }
        synchronized (userAccount.intern()) {
            boolean isExist = this.checkUserIsExistByUserAccountAndUserPassword(userAccount, userPassword);
            if (!isExist) {
                throw new BusinessException(ErrorCode.OPERATION_ERROR, "用户名已存在");
            }
            User user = new User();
            user.setUserAccount(userAccount);
            user.setUserPassword(userPassword);
            save(user);
            return user.getId();
        }
    }

    @Override
    public User getLoginUser(HttpServletRequest request) {
        User loginUser = (User) request.getSession().getAttribute(UserConstant.USER_CURRENT_STATE);
        if (loginUser == null) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "请先登录");
        }
        return loginUser;
    }

    public boolean checkUserIsExistByUserAccountAndUserPassword(String userAccount, String userPassword) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUserAccount, userAccount);
        queryWrapper.eq(User::getUserPassword, userPassword);
        User user = getOne(queryWrapper);
        return user == null;
    }
}




